class WebSorket {
    constructor(param) {
        this.websocket = null;
        this.isConnect = false;
        this.timeoutNum = null;
        this.isActivelyClose = false;
        this.param = param;
    }
    connect() {
        this.websocket = new WebSocket(this.param.url);
        this.initSocket(this.param);
    }
    initSocket(param) {
        this.isActivelyClose = false;
        this.websocket.onclose = () => {
            console.log('websocket连接关闭~' + this.param.url)
            this.isConnect = false;
            // 如果手动关闭则不进行重连
            if (!this.isActivelyClose) {
                this.reconnectSocket(param);
            }
        };

        this.websocket.onerror = (e) => {
            console.log('websocket发生异常~' + this.param.url + e)
            this.reconnectSocket(param);
        };

        this.websocket.onopen = () => {
            console.log('websocket已连接~ ' + this.param.url)
            this.isConnect = true;
            this.send(param.msg || "");
        };

        this.websocket.onmessage = (e) => {
            let _res;
            try {
                _res = JSON.parse(e.data);
            } catch (error) {
                _res = e.data;
            }
            param.callback(_res);
        };
    }

    reconnectSocket(param) {
        if (this.isConnect === true) {
            return false;
        }
        console.log('websocket 重新连接~ ')
        this.isConnect = true;
        this.timeoutNum && clearTimeout(this.timeoutNum);
        this.timeoutNum = setTimeout(() => {
            this.connect(param);
            this.isConnect = false;
        }, 1000);
    }

    /**
     * // websocket连接状态下才能进行send
     * @param {*} msg
     * 向服务send的消息
     */
    send(msg) {
        this.websocket.send(JSON.stringify(msg));
    }

    close() {
        this.isActivelyClose = true;
        if (this.websocket) {
            this.websocket.close();
        }
    }
}

export default WebSorket;
